//
//  PickNumSportListViewController.m
//  PlayBall
//
//  Created by Huawen Liu on 12-6-28.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "JddPickNumSportListViewController.h"
#import "JddShuangSeQiuListItemCell.h"
#import "JddShuangSeQiuNumber.h"
#import "JddPickNumSportViewController.h"
#import "JddViewControllerManager.h"
#import "UIFitButton.h"

@interface JddPickNumSportListViewController ()

@end

@implementation JddPickNumSportListViewController

@synthesize numberArray;
@synthesize numberListTable;
@synthesize issue;
@synthesize multipleTextField;
@synthesize noteAmountLabel;
@synthesize multipleLabel;
@synthesize moneyAmountLabel;
@synthesize bgScrollView;
@synthesize buybutton;
@synthesize playMethodTemp;
@synthesize playMethodLabel;
@synthesize endTimeLabel;
@synthesize endTime;
@synthesize amountLabel;

SHARED_VIEW_CONTROLLER_IMPLEMENT(JddPickNumSportListViewController);

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet *cs;
    cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789\n"]invertedSet];
    
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; //按cs分离出数组,数组按@""分离出字符串
    
    BOOL canChange = [string isEqualToString:filtered];
    
    if (!canChange) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:@"倍数只能输入数字"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil,nil ];
        [alert show];
        return NO;
    }
    if ([string length] + range.location>5) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:@"倍数不能大于99999"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil,nil ];
        [alert show];
        return NO;
    }
    
    if ([textField.text length]== 0) {
        if ([string isEqualToString:@"0"]) {
            return NO;
        }else {
            moneyAmountLabel.text = [NSString stringWithFormat:@"%0.0f彩豆", count * [[textField.text stringByReplacingCharactersInRange:range withString:string] intValue] * 2];
            return YES;
        }
    }
    if (range.location>=5) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:@"倍数不能大于99999"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil,nil ];
        [alert show];
        return NO;
    }else {
        moneyAmountLabel.text = [NSString stringWithFormat:@"%0.0f彩豆", count * [[textField.text stringByReplacingCharactersInRange:range withString:string] intValue] * 2];
        return YES;
    }
    
}

- (BOOL)textFieldShouldClear:(UITextField *)textField{
    return  YES;
}// clear button事件,返回NO过滤之
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    return YES;
}// 'return' key事件.返回NO过滤之

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapGesture:)];
    tapGestureRecognizer.delegate = self;
    [self.bgScrollView addGestureRecognizer:tapGestureRecognizer];
    [self.bgScrollView setContentOffset:CGPointMake(0,213)
                               animated:YES];    
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if ([touch.view isDescendantOfView:buybutton]) {
        return NO;
    }
    return YES;
}

- (IBAction)handleSingleTapGesture:(UITapGestureRecognizer*)sender{
    if ([multipleTextField.text length]==0) {
        multipleTextField.text = @"1";
    }
    [multipleTextField resignFirstResponder];
    multipleLabel.text = [NSString stringWithFormat:@"%@倍", multipleTextField.text];
    moneyAmountLabel.text = [NSString stringWithFormat:@"%3.0f彩豆", count * [multipleTextField.text floatValue] * 2];
    
    discountNo = [NSMutableString string];
    discountAmount = 0;
    [self.bgScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}

-(void)destoryView{
    playMethodTemp = [[NSMutableArray alloc] init];
    numberArray = [[NSMutableArray alloc] init];
    [multipleTextField resignFirstResponder];
    multipleTextField.text = @"1";
    playMethodLabel.text = @"";
}

- (IBAction)clearListButtonClicked:(id)sender{
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"玩球球" message:@"确认清空列表？" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
//    [alert show];
    if (numberArray.count >0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"确认清空列表？" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        [alert show];
    }else{
        
    }

}

- (void) alertView:(UIAlertView *)alertview clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if ([[alertview message] isEqualToString:@"您还没有登录，请先登录"]) {
        if (buttonIndex == 0) {
            [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_LOGIN
                                                                 fromController:self
                                                                     withObject:nil
                                                                    withObject2:nil];
        }
    } else if ([[alertview message] isEqualToString:@"确认购买？"]){
        if (buttonIndex==0) {
            [self sureToBuy];
        }
    }else if ([[alertview message] isEqualToString:@"确认清空列表？"]) {
        if (buttonIndex == 0) {
            playMethodTemp = [[NSMutableArray alloc] init];
            numberArray = [[NSMutableArray alloc] init];
            playMethodLabel.text = @"";
            count = 0;
            amountLabel.text = @"共选择0场比赛，截止时间:";
            endTimeLabel.text = @"";
            multipleTextField.text = @"1";
            noteAmountLabel.text = [NSString stringWithFormat:@"%3.0f注", count];
            multipleLabel.text = [NSString stringWithFormat:@"%@倍", multipleTextField.text];
            moneyAmountLabel.text = [NSString stringWithFormat:@"%3.0f彩豆", count * [multipleTextField.text floatValue] * 2];
            discountNo = [NSMutableString string];
            discountAmount = 0;
            [numberListTable reloadData];
        }
    }else if ([[alertview title] isEqualToString:@"退出提示"]) {
        if (buttonIndex == 0) {
            JddPickNumSportViewController *p = (JddPickNumSportViewController *)self.fromController;
            [p clearSelect:nil];
            [[JddViewControllerManager sharedInstance] popToBackViewController];
        }
    }
}

-(void)getTeamMaxSPWithSSqNumber:(JddSportNumber *)ssqNumber{
    NSLog(@"%@", ssqNumber.teamArray);
}

-(int)getNumWithSSqNumber:(JddSportNumber *)ssqNumber
{
    NSMutableArray *resultArr = ssqNumber.teamArray;
    NSMutableArray *spfArr = [resultArr objectAtIndex:0];
    NSMutableArray *rqspfArr = [resultArr objectAtIndex:1];
    NSMutableArray *zjcArr = [resultArr objectAtIndex:2];
    NSMutableArray *bqcArr = [resultArr objectAtIndex:3];
    NSMutableArray *bfArr = [resultArr objectAtIndex:4];
    int num1 = 0;
    if (spfArr.count > 1) {
        for (int i = 1; i < spfArr.count; i ++) {
            num1 += 1;
        }
    }
    if (rqspfArr.count > 1) {
        for (int i = 1; i < rqspfArr.count; i ++) {
            num1 += 1;
        }
    }
    if (zjcArr.count > 1) {
        for (int i = 1; i < zjcArr.count; i ++) {
            num1 += 1;
        }
    }
    if (bqcArr.count > 1) {
        for (int i = 1; i < bqcArr.count; i ++) {
            num1 += 1;
        }
    }
    if (bfArr.count > 1) {
        for (int i = 1; i < bfArr.count; i ++) {
            num1 += 1;
        }
    }
    return num1;
}

-(void)countMoney
{
    count = 0;
    for (NSString* platMethod in playMethodTemp) {
        if ([platMethod isEqualToString:@"单关"]) {
            for (int i = 0; i<[numberArray count]; ++i) {
                JddSportNumber *ssqNumber = [numberArray objectAtIndex:i ];
                int num1 = [ssqNumber.teamArray count];
                count += num1;
            }
        }
        else if ([platMethod isEqualToString:@"2串1"]) {
            if (self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_HH) {
                for (int i = 0; i<[numberArray count]; ++i) {
                    JddSportNumber *ssqNumber = [numberArray objectAtIndex:i ];
                    int num1 = [self getNumWithSSqNumber:ssqNumber];
                    for (int j = i+1; j<[numberArray count]; ++j) {
                        JddSportNumber *ssqNumber2 = [numberArray objectAtIndex:j];
                        int num2 = [self getNumWithSSqNumber:ssqNumber2];
                        count += num1*num2;
                    }
                }
            }else{
                for (int i = 0; i<[numberArray count]; ++i) {
                    JddSportNumber *ssqNumber = [numberArray objectAtIndex:i ];
                    int num1 = [ssqNumber.teamArray count];
//                    [self getTeamMaxSPWithSSqNumber:ssqNumber];
                    for (int j = i+1; j<[numberArray count]; ++j) {
                        JddSportNumber *ssqNumber2 = [numberArray objectAtIndex:j];
                        int num2 = [ssqNumber2.teamArray count];
                        count += num1*num2;
                    }
                }
            }
        } else if ([platMethod isEqualToString:@"3串1"]){
            if (self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_HH) {
                for (int i = 0; i<[numberArray count]; ++i) {
                    JddSportNumber *ssqNumber = [numberArray objectAtIndex:i ];
                    int num1 = [self getNumWithSSqNumber:ssqNumber];
                    for (int j = i+1; j<[numberArray count]; ++j) {
                        JddSportNumber *ssqNumber2 = [numberArray objectAtIndex:j];
                        int num2 = [self getNumWithSSqNumber:ssqNumber2];
                        for (int dan1 = j+1; dan1 < [numberArray count]; ++dan1) {
                            JddSportNumber *ssqNumber3 = [numberArray objectAtIndex:dan1];
                            int num3 = [self getNumWithSSqNumber:ssqNumber3];
                            count += num1*num2*num3;
                        }
                    }
                }
            }else{
                for (int i = 0; i<[numberArray count]; ++i) {
                    JddSportNumber *ssqNumber = [numberArray objectAtIndex:i ];
                    int num1 = [ssqNumber.teamArray count];
                    for (int j = i+1; j<[numberArray count]; ++j) {
                        JddSportNumber *ssqNumber2 = [numberArray objectAtIndex:j];
                        int num2 = [ssqNumber2.teamArray count];
                        for (int dan1 = j+1; dan1 < [numberArray count]; ++dan1) {
                            JddSportNumber *ssqNumber3 = [numberArray objectAtIndex:dan1];
                            int num3 = [ssqNumber3.teamArray count];
                            count += num1*num2*num3;
                        }
                    }
                }
            }
        } else if ([platMethod isEqualToString:@"4串1"]){
            if (self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_HH) {
                for (int i = 0; i<[numberArray count]; ++i) {
                    JddSportNumber *ssqNumber = [numberArray objectAtIndex:i ];
                    int num1 = [self getNumWithSSqNumber:ssqNumber];
                    for (int j = i+1; j<[numberArray count]; ++j) {
                        JddSportNumber *ssqNumber2 = [numberArray objectAtIndex:j];
                        int num2 = [self getNumWithSSqNumber:ssqNumber2];
                        for (int dan1 = j+1; dan1 < [numberArray count]; ++dan1) {
                            JddSportNumber *ssqNumber3 = [numberArray objectAtIndex:dan1];
                            int num3 = [self getNumWithSSqNumber:ssqNumber3];
                            for (int dan2 = dan1+1; dan2 < [numberArray count]; ++dan2) {
                                JddSportNumber *ssqNumber4 = [numberArray objectAtIndex:dan2];
                                int num4 = [self getNumWithSSqNumber:ssqNumber4];
                                count += num1*num2*num3*num4;
                            }
                        }
                    }
                }
            }else{
                for (int i = 0; i<[numberArray count]; ++i) {
                    JddSportNumber *ssqNumber = [numberArray objectAtIndex:i ];
                    int num1 = [ssqNumber.teamArray count];
                    for (int j = i+1; j<[numberArray count]; ++j) {
                        JddSportNumber *ssqNumber2 = [numberArray objectAtIndex:j];
                        int num2 = [ssqNumber2.teamArray count];
                        for (int dan1 = j+1; dan1 < [numberArray count]; ++dan1) {
                            JddSportNumber *ssqNumber3 = [numberArray objectAtIndex:dan1];
                            int num3 = [ssqNumber3.teamArray count];
                            for (int dan2 = dan1+1; dan2 < [numberArray count]; ++dan2) {
                                JddSportNumber *ssqNumber4 = [numberArray objectAtIndex:dan2];
                                int num4 = [ssqNumber4.teamArray count];
                                count += num1*num2*num3*num4;
                            }
                        }
                    }
                }
            }
        } else if ([platMethod isEqualToString:@"5串1"]){
            if (self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_HH) {
                for (int i = 0; i<[numberArray count]; ++i) {
                    JddSportNumber *ssqNumber = [numberArray objectAtIndex:i ];
                    int num1 = [self getNumWithSSqNumber:ssqNumber];
                    for (int j = i+1; j<[numberArray count]; ++j) {
                        JddSportNumber *ssqNumber2 = [numberArray objectAtIndex:j];
                        int num2 = [self getNumWithSSqNumber:ssqNumber2];
                        for (int dan1 = j+1; dan1 < [numberArray count]; ++dan1) {
                            JddSportNumber *ssqNumber3 = [numberArray objectAtIndex:dan1];
                            int num3 = [self getNumWithSSqNumber:ssqNumber3];
                            for (int dan2 = dan1+1; dan2 < [numberArray count]; ++dan2) {
                                JddSportNumber *ssqNumber4 = [numberArray objectAtIndex:dan2];
                                int num4 = [self getNumWithSSqNumber:ssqNumber4];
                                for (int dan3 = dan2+1; dan3 < [numberArray count]; ++dan3) {
                                    JddSportNumber *ssqNumber5 = [numberArray objectAtIndex:dan3];
                                    int num5 = [self getNumWithSSqNumber:ssqNumber5];
                                    count += num1*num2*num3*num4*num5;
                                }
                            }
                        }
                    }
                }
            }else{
                for (int i = 0; i<[numberArray count]; ++i) {
                    JddSportNumber *ssqNumber = [numberArray objectAtIndex:i ];
                    int num1 = [ssqNumber.teamArray count];
                    for (int j = i+1; j<[numberArray count]; ++j) {
                        JddSportNumber *ssqNumber2 = [numberArray objectAtIndex:j];
                        int num2 = [ssqNumber2.teamArray count];
                        for (int dan1 = j+1; dan1 < [numberArray count]; ++dan1) {
                            JddSportNumber *ssqNumber3 = [numberArray objectAtIndex:dan1];
                            int num3 = [ssqNumber3.teamArray count];
                            for (int dan2 = dan1+1; dan2 < [numberArray count]; ++dan2) {
                                JddSportNumber *ssqNumber4 = [numberArray objectAtIndex:dan2];
                                int num4 = [ssqNumber4.teamArray count];
                                for (int dan3 = dan2+1; dan3 < [numberArray count]; ++dan3) {
                                    JddSportNumber *ssqNumber5 = [numberArray objectAtIndex:dan3];
                                    int num5 = [ssqNumber5.teamArray count];
                                    count += num1*num2*num3*num4*num5;
                                }
                            }
                        }
                    }
                }
            }
        }else if ([platMethod isEqualToString:@"6串1"]){
            if (self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_HH) {
                for (int i = 0; i<[numberArray count]; ++i) {
                    JddSportNumber *ssqNumber = [numberArray objectAtIndex:i ];
                    int num1 = [self getNumWithSSqNumber:ssqNumber];
                    for (int j = i+1; j<[numberArray count]; ++j) {
                        JddSportNumber *ssqNumber2 = [numberArray objectAtIndex:j];
                        int num2 = [self getNumWithSSqNumber:ssqNumber2];
                        for (int dan1 = j+1; dan1 < [numberArray count]; ++dan1) {
                            JddSportNumber *ssqNumber3 = [numberArray objectAtIndex:dan1];
                            int num3 = [self getNumWithSSqNumber:ssqNumber3];
                            for (int dan2 = dan1+1; dan2 < [numberArray count]; ++dan2) {
                                JddSportNumber *ssqNumber4 = [numberArray objectAtIndex:dan2];
                                int num4 = [self getNumWithSSqNumber:ssqNumber4];
                                for (int dan3 = dan2+1; dan3 < [numberArray count]; ++dan3) {
                                    JddSportNumber *ssqNumber5 = [numberArray objectAtIndex:dan3];
                                    int num5 = [self getNumWithSSqNumber:ssqNumber5];
                                    for (int dan4 = dan3+1; dan4 < [numberArray count]; ++dan4) {
                                        JddSportNumber *ssqNumber6 = [numberArray objectAtIndex:dan4];
                                        int num6 = [self getNumWithSSqNumber:ssqNumber6];
                                        count += num1*num2*num3*num4*num5*num6;
                                    }
                                }
                            }
                        }
                    }
                }
            }else{
                for (int i = 0; i<[numberArray count]; ++i) {
                    JddSportNumber *ssqNumber = [numberArray objectAtIndex:i ];
                    int num1 = [ssqNumber.teamArray count];
                    for (int j = i+1; j<[numberArray count]; ++j) {
                        JddSportNumber *ssqNumber2 = [numberArray objectAtIndex:j];
                        int num2 = [ssqNumber2.teamArray count];
                        for (int dan1 = j+1; dan1 < [numberArray count]; ++dan1) {
                            JddSportNumber *ssqNumber3 = [numberArray objectAtIndex:dan1];
                            int num3 = [ssqNumber3.teamArray count];
                            for (int dan2 = dan1+1; dan2 < [numberArray count]; ++dan2) {
                                JddSportNumber *ssqNumber4 = [numberArray objectAtIndex:dan2];
                                int num4 = [ssqNumber4.teamArray count];
                                for (int dan3 = dan2+1; dan3 < [numberArray count]; ++dan3) {
                                    JddSportNumber *ssqNumber5 = [numberArray objectAtIndex:dan3];
                                    int num5 = [ssqNumber5.teamArray count];
                                    for (int dan4 = dan3+1; dan4 < [numberArray count]; ++dan4) {
                                        JddSportNumber *ssqNumber6 = [numberArray objectAtIndex:dan4];
                                        int num6 = [ssqNumber6.teamArray count];
                                        count += num1*num2*num3*num4*num5*num6;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }else if ([platMethod isEqualToString:@"7串1"]){
            if (self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_HH) {
                for (int i = 0; i<[numberArray count]; ++i) {
                    JddSportNumber *ssqNumber = [numberArray objectAtIndex:i ];
                    int num1 = [self getNumWithSSqNumber:ssqNumber];
                    for (int j = i+1; j<[numberArray count]; ++j) {
                        JddSportNumber *ssqNumber2 = [numberArray objectAtIndex:j];
                        int num2 = [self getNumWithSSqNumber:ssqNumber2];
                        for (int dan1 = j+1; dan1 < [numberArray count]; ++dan1) {
                            JddSportNumber *ssqNumber3 = [numberArray objectAtIndex:dan1];
                            int num3 = [self getNumWithSSqNumber:ssqNumber3];
                            for (int dan2 = dan1+1; dan2 < [numberArray count]; ++dan2) {
                                JddSportNumber *ssqNumber4 = [numberArray objectAtIndex:dan2];
                                int num4 = [self getNumWithSSqNumber:ssqNumber4];
                                for (int dan3 = dan2+1; dan3 < [numberArray count]; ++dan3) {
                                    JddSportNumber *ssqNumber5 = [numberArray objectAtIndex:dan3];
                                    int num5 = [self getNumWithSSqNumber:ssqNumber5];
                                    for (int dan4 = dan3+1; dan4 < [numberArray count]; ++dan4) {
                                        JddSportNumber *ssqNumber6 = [numberArray objectAtIndex:dan4];
                                        int num6 = [self getNumWithSSqNumber:ssqNumber6];
                                        for (int dan5 = dan4+1; dan5 < [numberArray count]; ++dan5) {
                                            JddSportNumber *ssqNumber7 = [numberArray objectAtIndex:dan5];
                                            int num7 = [self getNumWithSSqNumber:ssqNumber7];
                                            count += num1*num2*num3*num4*num5*num6*num7;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }else{
                for (int i = 0; i<[numberArray count]; ++i) {
                    JddSportNumber *ssqNumber = [numberArray objectAtIndex:i ];
                    int num1 = [ssqNumber.teamArray count];
                    for (int j = i+1; j<[numberArray count]; ++j) {
                        JddSportNumber *ssqNumber2 = [numberArray objectAtIndex:j];
                        int num2 = [ssqNumber2.teamArray count];
                        for (int dan1 = j+1; dan1 < [numberArray count]; ++dan1) {
                            JddSportNumber *ssqNumber3 = [numberArray objectAtIndex:dan1];
                            int num3 = [ssqNumber3.teamArray count];
                            for (int dan2 = dan1+1; dan2 < [numberArray count]; ++dan2) {
                                JddSportNumber *ssqNumber4 = [numberArray objectAtIndex:dan2];
                                int num4 = [ssqNumber4.teamArray count];
                                for (int dan3 = dan2+1; dan3 < [numberArray count]; ++dan3) {
                                    JddSportNumber *ssqNumber5 = [numberArray objectAtIndex:dan3];
                                    int num5 = [ssqNumber5.teamArray count];
                                    for (int dan4 = dan3+1; dan4 < [numberArray count]; ++dan4) {
                                        JddSportNumber *ssqNumber6 = [numberArray objectAtIndex:dan4];
                                        int num6 = [ssqNumber6.teamArray count];
                                        for (int dan5 = dan4+1; dan5 < [numberArray count]; ++dan5) {
                                            JddSportNumber *ssqNumber7 = [numberArray objectAtIndex:dan5];
                                            int num7 = [ssqNumber7.teamArray count];
                                            count += num1*num2*num3*num4*num5*num6*num7;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }else if ([platMethod isEqualToString:@"8串1"]){
            if (self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_HH) {
                for (int i = 0; i<[numberArray count]; ++i) {
                    JddSportNumber *ssqNumber = [numberArray objectAtIndex:i];
                    int num1 = [self getNumWithSSqNumber:ssqNumber];
                    for (int j = i+1; j<[numberArray count]; ++j) {
                        JddSportNumber *ssqNumber2 = [numberArray objectAtIndex:j];
                        int num2 = [self getNumWithSSqNumber:ssqNumber2];
                        for (int dan1 = j+1; dan1 < [numberArray count]; ++dan1) {
                            JddSportNumber *ssqNumber3 = [numberArray objectAtIndex:dan1];
                            int num3 = [self getNumWithSSqNumber:ssqNumber3];
                            for (int dan2 = dan1+1; dan2 < [numberArray count]; ++dan2) {
                                JddSportNumber *ssqNumber4 = [numberArray objectAtIndex:dan2];
                                int num4 = [self getNumWithSSqNumber:ssqNumber4];
                                for (int dan3 = dan2+1; dan3 < [numberArray count]; ++dan3) {
                                    JddSportNumber *ssqNumber5 = [numberArray objectAtIndex:dan3];
                                    int num5 = [self getNumWithSSqNumber:ssqNumber5];
                                    for (int dan4 = dan3+1; dan4 < [numberArray count]; ++dan4) {
                                        JddSportNumber *ssqNumber6 = [numberArray objectAtIndex:dan4];
                                        int num6 = [self getNumWithSSqNumber:ssqNumber6];
                                        for (int dan5 = dan4+1; dan5 < [numberArray count]; ++dan5) {
                                            JddSportNumber *ssqNumber7 = [numberArray objectAtIndex:dan5];
                                            int num7 = [self getNumWithSSqNumber:ssqNumber7];
                                            for (int dan6 = dan5+1; dan6 < [numberArray count]; ++dan6) {
                                                JddSportNumber *ssqNumber8 = [numberArray objectAtIndex:dan6];
                                                int num8 = [self getNumWithSSqNumber:ssqNumber8];
                                                count += num1*num2*num3*num4*num5*num6*num7*num8;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }else{
                for (int i = 0; i<[numberArray count]; ++i) {
                    JddSportNumber *ssqNumber = [numberArray objectAtIndex:i];
                    int num1 = [ssqNumber.teamArray count];
                    for (int j = i+1; j<[numberArray count]; ++j) {
                        JddSportNumber *ssqNumber2 = [numberArray objectAtIndex:j];
                        int num2 = [ssqNumber2.teamArray count];
                        for (int dan1 = j+1; dan1 < [numberArray count]; ++dan1) {
                            JddSportNumber *ssqNumber3 = [numberArray objectAtIndex:dan1];
                            int num3 = [ssqNumber3.teamArray count];
                            for (int dan2 = dan1+1; dan2 < [numberArray count]; ++dan2) {
                                JddSportNumber *ssqNumber4 = [numberArray objectAtIndex:dan2];
                                int num4 = [ssqNumber4.teamArray count];
                                for (int dan3 = dan2+1; dan3 < [numberArray count]; ++dan3) {
                                    JddSportNumber *ssqNumber5 = [numberArray objectAtIndex:dan3];
                                    int num5 = [ssqNumber5.teamArray count];
                                    for (int dan4 = dan3+1; dan4 < [numberArray count]; ++dan4) {
                                        JddSportNumber *ssqNumber6 = [numberArray objectAtIndex:dan4];
                                        int num6 = [ssqNumber6.teamArray count];
                                        for (int dan5 = dan4+1; dan5 < [numberArray count]; ++dan5) {
                                            JddSportNumber *ssqNumber7 = [numberArray objectAtIndex:dan5];
                                            int num7 = [ssqNumber7.teamArray count];
                                            for (int dan6 = dan5+1; dan6 < [numberArray count]; ++dan6) {
                                                JddSportNumber *ssqNumber8 = [numberArray objectAtIndex:dan6];
                                                int num8 = [ssqNumber8.teamArray count];
                                                count += num1*num2*num3*num4*num5*num6*num7*num8;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    noteAmountLabel.text = [NSString stringWithFormat:@"%3.0f注", count];
    multipleLabel.text = [NSString stringWithFormat:@"%@倍", multipleTextField.text];
    moneyAmountLabel.text = [NSString stringWithFormat:@"%3.0f彩豆", count * [multipleTextField.text floatValue] * 2];
    
    discountNo = [NSMutableString string];
    discountAmount = 0;
}

-(void)selectMatch:(NSArray*)match{
    playMethodTemp = [NSMutableArray arrayWithArray:match];
    NSMutableString *playMethodString = [NSMutableString string];
    for (NSString *playMethod1 in playMethodTemp) {
        [playMethodString appendString:playMethod1];
        [playMethodString appendString:@","];
        
    }
    playMethodLabel.text = playMethodString;
    
    [self countMoney];
}

- (IBAction)plusButtonClicked:(id)sender{
    NSInteger multiple = [multipleTextField.text intValue];
    if (multiple>99998) {
        return;
    }
    multipleTextField.text = [NSString stringWithFormat:@"%d", ++multiple];
    multipleLabel.text = [NSString stringWithFormat:@"%@倍", multipleTextField.text];
    moneyAmountLabel.text = [NSString stringWithFormat:@"%3.0f彩豆", count * [multipleTextField.text intValue] * 2];
}

- (IBAction)minusButtonClicked:(id)sender{
    NSInteger multiple = [multipleTextField.text intValue];
    if (multiple-- < 2) {
        multipleTextField.text = @"1";
        return;
    }
    
    multipleTextField.text = [NSString stringWithFormat:@"%d", multiple];
    multipleLabel.text = [NSString stringWithFormat:@"%@倍", multipleTextField.text];
    moneyAmountLabel.text = [NSString stringWithFormat:@"%3.0f彩豆", count * [multipleTextField.text floatValue] * 2];
}

- (NSMutableArray*)lotteryNumberList{
    NSMutableArray *paramNumberArray = [[NSMutableArray alloc] init];
    NSMutableString *lotteryNumberString = [NSMutableString string];
    NSMutableDictionary *numDic = [[NSMutableDictionary alloc] init];
    [numDic setObject:[NSNumber numberWithInt:self.playId] forKey:@"playid"];
    
    for (int i= 0; i<[numberArray count]; ++i) {
        JddSportNumber *ssqNumber = [numberArray objectAtIndex:i ];
        if(i!=0){
            [lotteryNumberString appendString:@","];
        }
        //131101001=
        NSString *code = ssqNumber.code;
        //混合
        if (self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_HH) {
            NSArray *codeArr = [ssqNumber.code componentsSeparatedByString:@"/"];
            code = [codeArr objectAtIndex:1];
        }
        [lotteryNumberString appendString:code];
        [lotteryNumberString appendString:@"="];
        //半全场
        NSMutableArray *bqcArray = [[NSMutableArray alloc] init];
        if (self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_BQC) {
            for(NSString *s in ssqNumber.teamArray){
                NSString *bqcStr = [NSString stringWithFormat:@"%@-%@", [s substringToIndex:1], [s substringFromIndex:1]];
                [bqcArray addObject:bqcStr];
            }
        }
        NSString* team1 = [ssqNumber.teamArray componentsJoinedByString:@"/"];
        NSMutableString *teamhh = [NSMutableString string];
        //比分
        if (self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_BF) {
            team1 = [team1 stringByReplacingOccurrencesOfString :@"胜其他"withString:@"9:0"];
            team1 = [team1 stringByReplacingOccurrencesOfString :@"平其他"withString:@"9:9"];
            team1 = [team1 stringByReplacingOccurrencesOfString :@"负其他"withString:@"0:9"];
        }
        //半全场
        if (self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_BQC) {
            team1 = [bqcArray componentsJoinedByString:@"/"];
            team1 = [team1 stringByReplacingOccurrencesOfString :@"胜"withString:@"3"];
            team1 = [team1 stringByReplacingOccurrencesOfString :@"平"withString:@"1"];
            team1 = [team1 stringByReplacingOccurrencesOfString :@"负"withString:@"0"];
        }
        //混合
        if (self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_HH) {
            BOOL bf = YES;
            NSMutableArray *resultArr = ssqNumber.teamArray;
            NSMutableArray *spfArr = [resultArr objectAtIndex:0];
            NSMutableArray *rqspfArr = [resultArr objectAtIndex:1];
            NSMutableArray *zjcArr = [resultArr objectAtIndex:2];
            NSMutableArray *bqcArr = [resultArr objectAtIndex:3];
            NSMutableArray *bfArr = [resultArr objectAtIndex:4];
            //让球胜平负
            if (rqspfArr.count > 1) {
                bf = NO;
                NSMutableArray *rqspfA = [NSMutableArray array];
                for(int i = 1; i < rqspfArr.count; i ++){
                    [rqspfA addObject:[rqspfArr objectAtIndex:i]];
                }
                NSString *rqspfContent = [rqspfA componentsJoinedByString:@"/"];
                rqspfContent = [rqspfContent stringByReplacingOccurrencesOfString :@"胜"withString:@"3"];
                rqspfContent = [rqspfContent stringByReplacingOccurrencesOfString :@"平"withString:@"1"];
                rqspfContent = [rqspfContent stringByReplacingOccurrencesOfString :@"负"withString:@"0"];
                rqspfContent = [rqspfContent stringByReplacingOccurrencesOfString :@" "withString:@""];
                [teamhh appendString:[NSString stringWithFormat:@"%@%@", @"1_", rqspfContent]];
            }
            //总进球
            if (zjcArr.count > 1) {
                NSString *tag = @"^2_";
                if (bf) {
                    tag = @"2_";
                    bf = NO;
                }
                NSMutableArray *zjcA = [NSMutableArray array];
                for(int i = 1; i < zjcArr.count; i ++){
                    [zjcA addObject:[zjcArr objectAtIndex:i]];
                }
                NSString *zjcContent = [zjcA componentsJoinedByString:@"/"];
                zjcContent = [zjcContent stringByReplacingOccurrencesOfString :@" "withString:@""];
                zjcContent = [zjcContent stringByReplacingOccurrencesOfString :@"7+"withString:@"7"];
                [teamhh appendString:[NSString stringWithFormat:@"%@%@", tag, zjcContent]];
            }
            //比分
            if (bfArr.count > 1) {
                NSString *tag = @"^3_";
                if (bf) {
                    tag = @"3_";
                    bf = NO;
                }
                NSMutableArray *bfA = [NSMutableArray array];
                for(int i = 1; i < bfArr.count; i ++){
                    [bfA addObject:[bfArr objectAtIndex:i]];
                }
                NSString *bfContent = [bfA componentsJoinedByString:@"/"];
                bfContent = [bfContent stringByReplacingOccurrencesOfString :@" "withString:@""];
                bfContent = [bfContent stringByReplacingOccurrencesOfString :@"胜其他"withString:@"9:0"];
                bfContent = [bfContent stringByReplacingOccurrencesOfString :@"平其他"withString:@"9:9"];
                bfContent = [bfContent stringByReplacingOccurrencesOfString :@"负其他"withString:@"0:9"];
                [teamhh appendString:[NSString stringWithFormat:@"%@%@", tag, bfContent]];
            }
            //半全场
            if (bqcArr.count > 1) {
                NSString *tag = @"^4_";
                if (bf) {
                    tag = @"4_";
                    bf = NO;
                }
                NSMutableArray *bqcA = [NSMutableArray array];
                for(int i = 1; i < bqcArr.count; i ++){
                    [bqcA addObject:[bqcArr objectAtIndex:i]];
                }
                NSString *bqcContent = [bqcA componentsJoinedByString:@"/"];
                bqcContent = [bqcContent stringByReplacingOccurrencesOfString :@" "withString:@""];
                bqcContent = [bqcContent stringByReplacingOccurrencesOfString :@"胜胜"withString:@"3-3"];
                bqcContent = [bqcContent stringByReplacingOccurrencesOfString :@"胜平"withString:@"3-1"];
                bqcContent = [bqcContent stringByReplacingOccurrencesOfString :@"胜负"withString:@"3-0"];
                bqcContent = [bqcContent stringByReplacingOccurrencesOfString :@"平胜"withString:@"1-3"];
                bqcContent = [bqcContent stringByReplacingOccurrencesOfString :@"平平"withString:@"1-1"];
                bqcContent = [bqcContent stringByReplacingOccurrencesOfString :@"平负"withString:@"1-0"];
                bqcContent = [bqcContent stringByReplacingOccurrencesOfString :@"负胜"withString:@"0-3"];
                bqcContent = [bqcContent stringByReplacingOccurrencesOfString :@"负平"withString:@"0-1"];
                bqcContent = [bqcContent stringByReplacingOccurrencesOfString :@"负负"withString:@"0-0"];
                [teamhh appendString:[NSString stringWithFormat:@"%@%@", tag, bqcContent]];
            }
            //胜平负
            if (spfArr.count > 1) {
                NSString *tag = @"^6_";
                if (bf) {
                    tag = @"6_";
                    bf = NO;
                }                NSMutableArray *spfA = [NSMutableArray array];
                for(int i = 1; i < spfArr.count; i ++){
                    [spfA addObject:[spfArr objectAtIndex:i]];
                }
                NSString *spfContent = [spfA componentsJoinedByString:@"/"];
                spfContent = [spfContent stringByReplacingOccurrencesOfString :@"胜"withString:@"3"];
                spfContent = [spfContent stringByReplacingOccurrencesOfString :@"平"withString:@"1"];
                spfContent = [spfContent stringByReplacingOccurrencesOfString :@"负"withString:@"0"];
                spfContent = [spfContent stringByReplacingOccurrencesOfString :@" "withString:@""];
                [teamhh appendString:[NSString stringWithFormat:@"%@%@", tag, spfContent]];
            }
            
        }
        //131101001=1
        //3 1 0 s p f
        if (self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_HH) {
            [lotteryNumberString appendString:teamhh];
        }else{
            [lotteryNumberString appendString:team1];
        }
    }
    //最后增加|
	//131101001=3/1,131101002=3/1/0|
	[lotteryNumberString appendString:@"|"];
	for (int i = 0; i < [playMethodTemp count]; i++) {
		NSString *playMethod = [playMethodTemp objectAtIndex:i];
		NSString *playMethod2 = [self getPlayMethodNameWithNameStr:playMethod];
        if (i > 0) {
            [lotteryNumberString appendString:@","];
        }
		[lotteryNumberString appendString:playMethod2];
	}
    if (self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_HH) {
        [lotteryNumberString appendString:@"|0|0"];
    }
	[numDic setObject:lotteryNumberString forKey:@"number"];
	[paramNumberArray addObject:numDic];
	return paramNumberArray;
}

-(NSString *)getPlayMethodNameWithNameStr:(NSString *)name
{
    NSString *playMethod2;
    if ([name isEqualToString:@"单关"]) {
        playMethod2 = @"1*1";
    }
    else if ([name isEqualToString:@"2串1"]) {
        playMethod2 = @"2*1";
    } else if ([name isEqualToString:@"3串1"]) {
        playMethod2 = @"3*1";
    }else if ([name isEqualToString:@"4串1"]) {
        playMethod2 = @"4*1";
    }else if ([name isEqualToString:@"5串1"]) {
        playMethod2 = @"5*1";
    }else if ([name isEqualToString:@"6串1"]) {
        playMethod2 = @"6*1";
    }else if ([name isEqualToString:@"7串1"]) {
        playMethod2 = @"7*1";
    }else if ([name isEqualToString:@"8串1"]) {
        playMethod2 = @"8*1";
    }
    return playMethod2;
}

- (void)BuyLotteryOperationResult:(NSData *)result{
    [JddCommonFunction setWaitingViewHidden:YES fromController:self];
    if (result) {
        
        NSDictionary *ReDic = [NSJSONSerialization JSONObjectWithData:result options:NSJSONReadingMutableContainers error:nil];
        if([[ReDic objectForKey:@"Code"] intValue] == 0)
        {
            
            // modify by rainy  修改投注使用接口不经过web
            NSString * fanganhao = [ReDic objectForKey:@"Data"];
            alert1 = [JddAlertView getInstanceView];
            alert1.lssueString = fanganhao;
            alert1.moneylable = moneyAmountLabel.text;
            [alert1 alertBuyMessage:@"竞彩足球"];
            
//            NSString*checkUrlString = [NSString stringWithFormat:@"%@?ck=%@",BASE_SERVER_IOS_BUY_LOTTERY_URL,[ReDic objectForKey:@"Data"]];
//            NSURL *checkUrl = [NSURL URLWithString:checkUrlString];
//            [[UIApplication sharedApplication] openURL:checkUrl];
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"购彩失败"                                                  message:[ReDic objectForKey:@"Data"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil,nil ];
            [alert show];
        }
//        [JddCommonFunction updateBuyLotteryListWithLotteryId:LOTTERY_TYPE_ID_FOOTBALL];
    }
}

-(void)QueryUserInfoOperationResult:(NSData *)result{
    if (result) {
        if ([JddDataParser parseUserInfoResult:result]) {
            
            
        }else {
            if ([[JddViewControllerManager sharedInstance].navigationController topViewController] == [JddPickNumSportListViewController sharedInstance]) {
                [JddCommonFunction messageBoxWithMessage:[JddError sharedInstance].message];
            }
        }
    }
}

- (void)QueryDiscountOperationResult:(NSData *)result{
    if (result) {
        
        if ([JddDataParser parseDiscountResult:result discountNo:discountNo discountAmount:discountAmount]){
            JddQueryUserInfoOperation *operation = [[JddQueryUserInfoOperation alloc] init];
            operation.delegate = self;
            [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
            
        }
        else {
            [JddCommonFunction messageBoxWithMessage:[JddError sharedInstance].message];
            [JddCommonFunction setWaitingViewHidden:YES fromController:self];
        }
    }
    
}

-(void)selectPlayMethodButtonClicked:(id)sender{
    NSArray *playMethod = [[NSArray alloc] init];
    if (([numberArray count] > 1) || ([numberArray count] > 0 && isContainSingle == true/* && self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_BF*/)) {
        if(self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_BF){
            playMethod = [[NSArray alloc] initWithObjects:@"单关", @"2串1",@"3串1",@"4串1", nil];
        }else{
            if(isContainSingle){
                playMethod = [[NSArray alloc] initWithObjects:@"单关",@"2串1",@"3串1",@"4串1",@"5串1",@"6串1",@"7串1",@"8串1", nil];
            }else{
                playMethod = [[NSArray alloc] initWithObjects:@"2串1",@"3串1",@"4串1",@"5串1",@"6串1",@"7串1",@"8串1", nil];
            }
        }
        int length = numberArray.count;
        //最多只有8串1
        if (length > 7) {
            length = 8;
        }
        //混合
        if (self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_HH) {
            BOOL zjq, bqc, bf;
            for(JddSportNumber *ssqNumber in numberArray)
                {
                NSMutableArray *resultArr = ssqNumber.teamArray;
                NSMutableArray *zjcArr = [resultArr objectAtIndex:2];
                NSMutableArray *bqcArr = [resultArr objectAtIndex:3];
                NSMutableArray *bfArr = [resultArr objectAtIndex:4];
                if (zjcArr.count > 1) {
                    zjq = YES;
                }
                if (bqcArr.count > 1) {
                    bqc = YES;
                }
                if (bfArr.count > 1) {
                    bf = YES;
                }
                }
            //如果是半全场，比分，最多只有4串1
            if(bqc || bf){
                if(length>3){
                    length = 4;
                }
            }
            //如果是总进球，最多只有6串1
            if(zjq){
                if(length>5){
                    length = 6;
                }
            }
        }else{
            //如果是半全场，比分，最多只有4串1
            if(self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_BF || self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_BQC){
                if(length>3){
                    length = 4;
                }
            }
            //如果是总进球，最多只有6串1
            if(self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_ZJQ){
                if(length>5){
                    length = 6;
                }
            }
        }
        NSMutableArray *newPlayMethod = [[NSMutableArray alloc] init];
//        if (!(self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_BF)) {
//            length -= 1;
//        }
        if(isContainSingle == NO){
            length -= 1;
        }
        for(int i = 0; i < length; i ++){
            [newPlayMethod addObject:[playMethod objectAtIndex:i]];
        }
        playMethod = newPlayMethod;
        
        CGRect rect = [[UIScreen mainScreen] bounds];
        rect.origin.x = (rect.size.width - 251) / 2.0;
        rect.origin.y = (rect.size.height - 139) / 2.0;
        rect.size.width = 251.0;
        rect.size.height = 139.0;
        comboBoxView = [JddComboBoxView getInstanceView];
        [comboBoxView setFrame:rect];
        comboBoxView.dataArray = [NSArray arrayWithArray:playMethod];
        comboBoxView.tempArray = [NSMutableArray arrayWithArray:playMethodTemp];
        comboBoxView.commitLabel.hidden = YES;
        comboBoxView.type = 0;
        comboBoxView.delegate = self;
        [comboBoxView show];
    } else {
        NSString *message = @"请至少选择两场比赛！";
        if (self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_BF) {
            message = @"请至少选择一场比赛！";
        }
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:message
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil ];
        [alert show];
    }
}


-(BOOL)checkValue{
    if ([multipleTextField.text intValue]>99999) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:@"倍数不能大于99999"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil,nil ];
        [alert show];
        multipleTextField.text =  @"99999";
        return NO;
    }else if ((count * [multipleTextField.text intValue] * 2) > 2000000 ){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:@"单个方案的总金额必须在2彩豆至2000000彩豆之间"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil,nil ];
        [alert show];
        return NO;
    }
    return YES;
}

-(void)sureToBuy{
    if ([self checkValue]) {
        [JddCommonFunction setWaitingViewHidden:NO fromController:self];
        NSNumber *multiple = [NSNumber numberWithInt:[multipleTextField.text intValue]];
        JddBuyLotteryOperation *operation2 = [[JddBuyLotteryOperation alloc] init];
        operation2.delegate = self;
        operation2.lotteryTypeId = [NSNumber numberWithInt:LOTTERY_TYPE_ID_FOOTBALL];
        operation2.issueId = [NSNumber numberWithInt:2016];
        operation2.multiple = multiple;
        operation2.moneyAmount = [NSNumber numberWithFloat:count * [multipleTextField.text floatValue] * 2];
        operation2.lotteryNumber = [self lotteryNumberList];
        [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation2];
    }
}

- (IBAction)buyButtonClicked:(id)sender{
    int multiple = [multipleTextField.text intValue];
    if (![JddCommonFunction isLogin]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:@"您还没有登录，请先登录"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:@"取消",nil ];
        [alert show];
    } else {
        if (!agreeLotteryForFooterView.checkBoxBtn.selected) {
            [JddCommonFunction messageBoxWithMessage:BUY_NOT_READ];
        }
        else if ([numberArray count]==0) {
            [JddCommonFunction messageBoxWithMessage:@"购买内容为空，请先选择投注场次"];
        }else if ([numberArray count]==1 && isContainSingle == false/* && self.playId != LOTTERY_PLAY_METHOD_FOOTBALL_BF*/){
            [JddCommonFunction messageBoxWithMessage:@"请至少选择2场比赛"];
        }
        else if (multiple == 0) {
            [JddCommonFunction messageBoxWithMessage:MULTIPLE_Tip];
        }
        else {
            if ([playMethodTemp count]==0) {
                [JddCommonFunction messageBoxWithMessage:@"请至少选择一种过关方式"];
            }else {
                [self sureToBuy];
                /*
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"玩球球"
                                                                message:@"确认购买？"
                                                               delegate:self
                                                      cancelButtonTitle:@"确定"
                                                      otherButtonTitles:@"取消",nil ];
                [alert show];*/
                
            }
        }
    }
}

- (IBAction)continueSelectNumberButtonClicked:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)textFiledDidEnd:(id)sender {
    [multipleTextField resignFirstResponder];
}

- (IBAction)coBuyBtnClick:(id)sender {
    int multiple = [multipleTextField.text intValue];
    if (![JddCommonFunction isLogin]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:@"您还没有登录，请先登录"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:@"取消",nil ];
        [alert show];
    } else {
        if (!agreeLotteryForFooterView.checkBoxBtn.selected) {
            [JddCommonFunction messageBoxWithMessage:BUY_NOT_READ];
        }
        else if ([numberArray count]==0) {
            [JddCommonFunction messageBoxWithMessage:@"购买内容为空，请先选择投注场次"];
        }else if ([numberArray count]==1 && isContainSingle == false/* && self.playId != LOTTERY_PLAY_METHOD_FOOTBALL_BF*/){
            [JddCommonFunction messageBoxWithMessage:@"请至少选择2场比赛"];
        }
        else if (multiple == 0) {
            [JddCommonFunction messageBoxWithMessage:MULTIPLE_Tip];
        }else if ((count * [multipleTextField.text intValue] * 2) > 2000000 ){
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                            message:@"单个方案的总金额必须在2彩豆至2000000彩豆之间"
                                                           delegate:nil
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil,nil ];
            [alert show];
        }
        else {
            if ([playMethodTemp count]==0) {
                [JddCommonFunction messageBoxWithMessage:@"请至少选择一种过关方式"];
            }else {
                [FaQiHeMaiViewController sharedInstance].bs = [multipleTextField.text intValue];
                [FaQiHeMaiViewController sharedInstance].lotID = LOTTERY_TYPE_ID_FOOTBALL;
                [FaQiHeMaiViewController sharedInstance].issueID = @"2016";
                [FaQiHeMaiViewController sharedInstance].issueName = @"2016";
                [FaQiHeMaiViewController sharedInstance].issueNameTitle = [NSString stringWithFormat:@"竞彩足球合买"];
                [FaQiHeMaiViewController sharedInstance].allMoney = count * [multipleTextField.text floatValue] * 2;
                [FaQiHeMaiViewController sharedInstance].lotteryNumber = [self lotteryNumberList];
                [[JddViewControllerManager sharedInstance].navigationController pushViewController:[FaQiHeMaiViewController sharedInstance] animated:YES];
            }
        }
    }
}

- (void)SportListItemCellDeleteButtonClicked:(NSNumber *)rowNumber{
    for (int i = [rowNumber intValue] + 1; i < [numberArray count]; ++i) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        UITableViewCell *cell = [numberListTable cellForRowAtIndexPath:indexPath];
        cell.tag--;
    }
    [numberArray removeObjectAtIndex:[rowNumber intValue]];
    self.amountLabel.text = [NSString stringWithFormat:@"共选择%d场,截止时间:",[numberArray count]];
    [numberListTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[rowNumber intValue] inSection:0]] withRowAnimation:YES];
    if ([numberArray count]>1) {
        JddSportNumber *sportnum1 = [numberArray objectAtIndex:0];
        NSDate *date1 = [JddCommonFunction dateFromString:sportnum1.endTime];
        for (JddSportNumber *sportnum in numberArray) {
            NSDate *date2 = [JddCommonFunction dateFromString:sportnum.endTime];
            date1 =  [date2 earlierDate:date1];
        }
        endTime = date1;
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
        self.endTimeLabel.text = [dateFormatter stringFromDate:endTime];
    } else if ([numberArray count]==1){
        JddSportNumber *sportnum1 = [numberArray objectAtIndex:0];
        NSDate *date1 = [JddCommonFunction dateFromString:sportnum1.endTime];
        endTime = date1;
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
        self.endTimeLabel.text = [dateFormatter stringFromDate:endTime];
        
    }else {
        self.endTimeLabel.text = @"";
    }
    playMethodTemp = [[NSMutableArray alloc]init];
    playMethodLabel.text = @"";
    [self countMoney];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return DELETE;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [numberArray count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_HH) {
        JddSportListItemHHCell *cell = [self getHHCellWithIndexPath:indexPath];
        return 40+cell.contentLbl.frame.size.height;
    }//总进球/比分过关/半全场
    else if (self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_ZJQ || self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_BF || self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_BQC){
        JddSportListItemZJQCell *cell = [self getZJQ_BF_BQC_cellWithIndexPath:indexPath];
        return 40 + cell.numbersLbl.frame.size.height;
    }
    else{
        return 60.0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //让球胜平负，胜平负
    if (self.playId == LOTTERY_PLAY_METHOD_FOOTBALL || self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_SPF) {
        JddSportListItemCell *cell = [JddSportListItemCell getInstanceView];
        JddSportNumber *ssqNumber = [numberArray objectAtIndex:indexPath.row];
        cell.HTeam.text = ssqNumber.Hteam;
        cell.VTeam.text = ssqNumber.Vteam;
        for (int b=0; b<[ssqNumber.teamArray count]; ++b) {
            NSString *sptemp = [ssqNumber.teamArray objectAtIndex:b];
            switch ([sptemp intValue]) {
                case 0:
                    cell.sp3.image = [UIImage imageNamed:@"sport_select.png"];
                    break;
                case 1:
                    cell.sp2.image = [UIImage imageNamed:@"sport_select.png"];
                    break;
                case 3:
                    cell.sp.image = [UIImage imageNamed:@"sport_select.png"];
                    break;
                default:
                    break;
            }
        }
        cell.tag = indexPath.row;
        cell.delegate = self;
        return cell;
    }
    //总进球/比分过关/半全场
    else if (self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_ZJQ || self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_BF || self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_BQC){
        JddSportListItemZJQCell *cell = [self getZJQ_BF_BQC_cellWithIndexPath:indexPath];
        return cell;
    }else{
        JddSportListItemHHCell *cell = [self getHHCellWithIndexPath:indexPath];
        return cell;
    }
}

-(JddSportListItemZJQCell *)getZJQ_BF_BQC_cellWithIndexPath:(NSIndexPath *)indexPath{
    JddSportListItemZJQCell *cell = [JddSportListItemZJQCell getInstanceView];
    JddSportNumber *ssqNumber = [numberArray objectAtIndex:indexPath.row];
    cell.HTeam.text = ssqNumber.Hteam;
    cell.VTeam.text = ssqNumber.Vteam;
    NSMutableArray *numbersArr = [NSMutableArray arrayWithArray:[JddCommonFunction sortArray:ssqNumber.teamArray]];
    
    if ([[numbersArr lastObject] isEqualToString:@"7"]) {
        [numbersArr removeLastObject];
        [numbersArr addObject:@"7+"];
    }
    NSString *numbersStr = [numbersArr componentsJoinedByString:@","];
    if (self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_BQC) {
        numbersStr = [numbersArr componentsJoinedByString:@" "];
    }
    cell.numbersLbl.text = numbersStr;
    cell.tag = indexPath.row;
    cell.delegate = self;
    [self setHeightWithLable:cell.numbersLbl];
    return cell;
}
//混合玩法确认购彩的cell
-(JddSportListItemHHCell *)getHHCellWithIndexPath:(NSIndexPath *)indexPath
{
    JddSportListItemHHCell *cell = [JddSportListItemHHCell getInstanceView];
    JddSportNumber *ssqNumber = [numberArray objectAtIndex:indexPath.row];
    cell.teamLbl.text = [NSString stringWithFormat:@"%@ VS %@", ssqNumber.Hteam,ssqNumber.Vteam];
    NSArray *codeArr = [ssqNumber.code componentsSeparatedByString:@"/"];
    cell.wkLbl.text = [codeArr objectAtIndex:0];
    
    NSString *fontA = @"<font color='#1E90FF'>";
    NSString *fontB = @"</font>";
    NSMutableString *resultStr = [NSMutableString string];
    
    NSMutableArray *resultArr = ssqNumber.teamArray;
    NSMutableArray *spfArr = [resultArr objectAtIndex:0];
    NSMutableArray *rqspfArr = [resultArr objectAtIndex:1];
    NSMutableArray *zjcArr = [resultArr objectAtIndex:2];
    NSMutableArray *bqcArr = [resultArr objectAtIndex:3];
    NSMutableArray *bfArr = [resultArr objectAtIndex:4];
    
    if (spfArr.count > 1) {
        NSString *spfStr = [spfArr componentsJoinedByString:@""];
        NSArray *spfA = [spfStr componentsSeparatedByString:@":"];
        NSString *spfTitle = [spfA objectAtIndex:0];
        NSString *spfContent = [spfA objectAtIndex:1];
        [resultStr appendString:[NSString stringWithFormat:@"%@%@:%@%@\n", fontA, spfTitle, fontB, spfContent]];
    }
    if (rqspfArr.count > 1) {
        NSString *rqspfStr = [rqspfArr componentsJoinedByString:@""];
        NSArray *rqspfA = [rqspfStr componentsSeparatedByString:@":"];
        NSString *rqspfTitle = [rqspfA objectAtIndex:0];
        NSString *rqspfContent = [rqspfA objectAtIndex:1];
        [resultStr appendString:[NSString stringWithFormat:@"%@%@:%@%@\n", fontA, rqspfTitle, fontB, rqspfContent]];
    }
    if (zjcArr.count > 1) {
        NSString *zjcStr = [zjcArr componentsJoinedByString:@""];
        NSArray *zjcA = [zjcStr componentsSeparatedByString:@":"];
        NSString *zjcTitle = [zjcA objectAtIndex:0];
        NSString *zjcContent = [zjcA objectAtIndex:1];
        [resultStr appendString:[NSString stringWithFormat:@"%@%@:%@%@\n", fontA, zjcTitle, fontB, zjcContent]];
    }
    if (bqcArr.count > 1) {
        NSString *bqcStr = [bqcArr componentsJoinedByString:@""];
        NSArray *bqcA = [bqcStr componentsSeparatedByString:@":"];
        NSString *bqcTitle = [bqcA objectAtIndex:0];
        NSString *bqcContent = [bqcA objectAtIndex:1];
        [resultStr appendString:[NSString stringWithFormat:@"%@%@:%@%@\n", fontA, bqcTitle, fontB, bqcContent]];
    }
    if (bfArr.count > 1) {
        NSString *bfTitle = [bfArr objectAtIndex:0];
        NSMutableString *bfContent = [NSMutableString string];
        for (int i = 1; i < bfArr.count; i ++) {
            [bfContent appendString:[bfArr objectAtIndex:i]];
        }
        [resultStr appendString:[NSString stringWithFormat:@"%@%@%@%@", fontA, bfTitle, fontB, bfContent]];
    }

    cell.contentLbl.attributedText = [JddCommonFunction convertHTMLFontRedWithResult:resultStr AndCellLabel:cell.contentLbl];
    [self setHeightWithLable:cell.contentLbl];
        
    cell.tag = indexPath.row;
    cell.delegate = self;
    return cell;
}

-(void)setHeightWithLable:(UILabel *) lbl
{
    lbl.numberOfLines = 0;
    CGRect frame = lbl.frame;
    UIFont *font = lbl.font;
    CGFloat width = frame.size.width;
    NSString *result = lbl.text;
    lbl.lineBreakMode = NSLineBreakByWordWrapping;
    //替换html使不影响计算高度
    result = [result stringByReplacingOccurrencesOfString:@"<font color='#1E90FF'>" withString:@""];
    result = [result stringByReplacingOccurrencesOfString:@"</font>" withString:@""];
    
    CGSize size = [result sizeWithFont:font constrainedToSize:CGSizeMake(width, MAXFLOAT)lineBreakMode:NSLineBreakByWordWrapping];
    frame.size.height = size.height;
    [lbl setFrame:frame];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //self.navigationItem.hidesBackButton = YES;
    
//    UIFitButton *backButton = [UIFitButton buttonWithType:UIButtonTypeCustom];
//    backButton.isleftBtn = YES;
//    backButton.frame = CGRectMake(0.0, 0.0, 12, 24);
//    [backButton setImage:[UIImage imageNamed:@"navbar_back"] forState:UIControlStateNormal];
//    [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
//    UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
//    temporaryBarButtonItem.style = UIBarButtonItemStylePlain;
//    self.navigationItem.leftBarButtonItem = temporaryBarButtonItem;
    
    agreeLotteryForFooterView = [JddAgreeLotteryForFooterView getInstanceView];
    agreeLotteryForFooterView.fromViewController = self;
    numberListTable.tableFooterView = agreeLotteryForFooterView;
//    [self.navigationItem setTitle:PAGE_SPORT_PICK_NUMBER_LIST];
    
        UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
        customLab.backgroundColor = [UIColor clearColor];
        [customLab setText:PAGE_SPORT_PICK_NUMBER_LIST];
        customLab.textAlignment = NSTextAlignmentCenter;

        [customLab setTextColor:[UIColor whiteColor]];
        customLab.font = [UIFont systemFontOfSize:19];
        self.navigationItem.titleView = customLab;
    
    playMethodTemp = [[NSMutableArray alloc] init];
    playMethodLabel.text = @"";
    

    [numberListTable setSeparatorColor:SeparatorLineColor];
    if (IOS7) {
        [numberListTable setSeparatorInset:UIEdgeInsetsZero];
    }
//    self.amountLabel.font = [UIFont systemFontOfSize:13];
}
 
-(NSArray*)sortArray:(NSArray *)array{
    
    NSArray *sortedArray = [array sortedArrayUsingComparator: ^(id obj1, id obj2) {
        JddSportNumber *sportnum1= (JddSportNumber*)obj1;
        JddSportNumber *sportnum2= (JddSportNumber*)obj2;
        if ([sportnum1.code integerValue] > [sportnum2.code integerValue]) {
            return (NSComparisonResult)NSOrderedDescending;//递减
        }
        
        if ([sportnum1.code integerValue] < [sportnum2.code integerValue]) {
            return (NSComparisonResult)NSOrderedAscending;//上升
        }
        return (NSComparisonResult)NSOrderedSame;
    }];
    return sortedArray;
}

-(void)swapNum:(JddSportNumber*)sportnum1 sportNum:(JddSportNumber*)sportnum2{
    JddSportNumber* sportnumtemp = [[JddSportNumber alloc] init];
    sportnumtemp = sportnum1;
    sportnum1 = sportnum2;
    sportnum2 = sportnumtemp;
}

-(void)back
{
    if (numberArray.count >0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"退出提示"
                                                        message:@"退出后您的投注将不会保存，确认继续?"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:@"取消", nil];
        [alert show];
        
    }else{
        JddPickNumSportViewController *p = (JddPickNumSportViewController *)self.fromController;
        [p clearSelect:nil];
        [[JddViewControllerManager sharedInstance] popToBackViewController];
    }
}

-(void)setBackBtnAction
{
    UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
    temporaryBarButtonItem.target = self;
    temporaryBarButtonItem.title = @"竞彩足球";
    temporaryBarButtonItem.action = @selector(back);
    self.navigationItem.leftBarButtonItem = temporaryBarButtonItem;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"JCZQ-list"];

//    [self setBackBtnAction];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    self.endTimeLabel.text = [dateFormatter stringFromDate:endTime];
    self.amountLabel.text = [NSString stringWithFormat:@"共选择%d场,截止时间:",[numberArray count]];
    
    isContainSingle = NO;
    [playMethodTemp removeAllObjects];
    if (self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_HH) {
        
    }else if (self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_ZJQ){
        isContainSingle = YES;
        //总进球
        if ([numberArray count] == 1) {
            playMethodLabel.text = @"单关";
            [playMethodTemp addObject:@"单关"];
        }else
        if ([numberArray count] > 6) {
            playMethodLabel.text = [NSString stringWithFormat:@"%d串1,",6];
            [playMethodTemp addObject:[NSString stringWithFormat:@"%d串1",6]];
        }else{
            playMethodLabel.text = [NSString stringWithFormat:@"%d串1,",[numberArray count]];
            [playMethodTemp addObject:[NSString stringWithFormat:@"%d串1",[numberArray count]]];
        }
    }else if (self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_BQC){
        isContainSingle = YES;
        //半全场
        if ([numberArray count] == 1) {
            playMethodLabel.text = @"单关";
            [playMethodTemp addObject:@"单关"];
        }else
        if ([numberArray count] > 4) {
            playMethodLabel.text = [NSString stringWithFormat:@"%d串1,",4];
            [playMethodTemp addObject:[NSString stringWithFormat:@"%d串1",4]];
        }else{
            playMethodLabel.text = [NSString stringWithFormat:@"%d串1,",[numberArray count]];
            [playMethodTemp addObject:[NSString stringWithFormat:@"%d串1",[numberArray count]]];
        }
    }else if (self.playId == LOTTERY_PLAY_METHOD_FOOTBALL_BF ){
        isContainSingle = YES;
        //比分
        if ([numberArray count] == 1) {
            playMethodLabel.text = @"单关";
            [playMethodTemp addObject:@"单关"];
        }else
        if ([numberArray count] > 4) {
            playMethodLabel.text = [NSString stringWithFormat:@"%d串1,",4];
            [playMethodTemp addObject:[NSString stringWithFormat:@"%d串1",4]];
        }else{
            playMethodLabel.text = [NSString stringWithFormat:@"%d串1,",[numberArray count]];
            [playMethodTemp addObject:[NSString stringWithFormat:@"%d串1",[numberArray count]]];
        }
    }else if (LOTTERY_PLAY_METHOD_FOOTBALL == self.playId || LOTTERY_PLAY_METHOD_FOOTBALL_SPF == self.playId){
        isContainSingle = YES;
        for (JddSportNumber *ssqNumber in numberArray) {
            if(ssqNumber.isSingle == 0){
                isContainSingle = NO;
            }
        }
        //让球胜平负
        if ([numberArray count] == 1) {
            playMethodLabel.text = @"单关";
            [playMethodTemp addObject:@"单关"];
        }else
        if ([numberArray count] > 8) {
            playMethodLabel.text = [NSString stringWithFormat:@"%d串1,",8];
            [playMethodTemp addObject:[NSString stringWithFormat:@"%d串1",8]];
        }else{
            playMethodLabel.text = [NSString stringWithFormat:@"%d串1,",[numberArray count]];
            [playMethodTemp addObject:[NSString stringWithFormat:@"%d串1",[numberArray count]]];
        }
    }
    
    
    
    [self countMoney];
    [numberListTable reloadData];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [MobClick endLogPageView:@"JCZQ-list"];

    if ([multipleTextField.text length]==0) {
        multipleTextField.text = @"1";
    }
    [multipleTextField resignFirstResponder];
}

@end
